package com.sskj.givecoin.p.activity;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;
import com.sskj.givecoin.MyAppLication;
import com.sskj.givecoin.common.Constants;
import com.sskj.givecoin.m.HttpConfig;
import com.sskj.givecoin.m.bean.AppVersionBean;
import com.sskj.givecoin.m.bean.HttpData;
import com.sskj.givecoin.m.bean.UserInfo;
import com.sskj.givecoin.m.http.JsonCallBack;
import com.sskj.givecoin.p.base.BasePresenter;
import com.sskj.givecoin.utils.APKVersionCodeUtils;
import com.sskj.givecoin.utils.SPUtils;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.fragment.MineFragment;


/**
 * Created by Lee on 2018/1/25 0025.
 */

public class MinePresenter extends BasePresenter<MineFragment> {

    public void requestUserInfo(String mobile) {
        OkGo.<HttpData<UserInfo>>post(HttpConfig.BASE_URL + HttpConfig.GET_USER_INFO)
                .params("mobile", mobile)
                .cacheKey(HttpConfig.BASE_URL + HttpConfig.GET_USER_INFO)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(new JsonCallBack<HttpData<UserInfo>>(this, false) {
                    @Override
                    public void _onSuccess(Response<HttpData<UserInfo>> response) {
                        HttpData<UserInfo> httpData = response.body();
                        if (httpData.getStatus() == 200) {
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_IS_SHOP, httpData.getData().is_shop);
                            mView.updateUserTitle(httpData.getData());
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<HttpData<UserInfo>> response) {
                        super.onCacheSuccess(response);
                        HttpData<UserInfo> httpData = response.body();
                        if (httpData.getStatus() == 200) {
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_IS_SHOP, httpData.getData().is_shop);
                            mView.updateUserTitle(httpData.getData());
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        mView.stopRefresh();
                    }
                });
    }

    public void sign() {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SIGN)
                .params("mobile", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData<CoinGetBean> httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.signSuccess();
                            ToastUtil.showLong(httpListData.getMsg());
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }

                });
    }

    class CoinGetBean {
        String bz;
        String num;
    }


    public void getVersion() {
        OkGo.<HttpData<AppVersionBean>>post(HttpConfig.BASE_URL + HttpConfig.VERSION)
                .tag(mView)
                .params("version", APKVersionCodeUtils.getVerName(MyAppLication.getApplication()))
                .params("type", "1")
                .tag(mView)
                .execute(new JsonCallBack<HttpData<AppVersionBean>>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData<AppVersionBean>> response) {
                        HttpData<AppVersionBean> httpData = response.body();
                        if (httpData.getStatus() == 200) {
                            ToastUtil.showShort(httpData.getMsg());
                            mView.onVersionDataSuccess(httpData.getData());
                        }else {
                            ToastUtil.showShort(httpData.getMsg());

                        }
                    }

                    @Override
                    public void onError(Response<HttpData<AppVersionBean>> response) {
                        super.onError(response);
                    }
                });

    }
}
